package gui;


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.JToolBar;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.SwingUtilities;

import program.DatabaseConnection;

import drawingtools.ActorTool;
import drawingtools.BorderAreaUsesTool;
import drawingtools.ExtendTool;
import drawingtools.InheritanceTool;
import drawingtools.LineTool;
import drawingtools.MoveTool;
import drawingtools.RealizeTool;
import drawingtools.RefineTool;
import drawingtools.SelectionTool;
import drawingtools.UseCaseDashedTool;
import drawingtools.UseCaseTool;
import drawingtools.IncludeTool;





/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MainWindow extends javax.swing.JFrame {
	private JMenuBar MainWindowMenuBar;
	private JMenuItem MenuFile_Open;
	private JButton ToolBarLineButton;
	private JToolBar ToolBar;
	private JMenuItem MenuTools_Line;
	private JMenu MenuTools;
	private JMenu MenuWindow;
	private JMenuItem MenuFile_Close;
	private JSeparator MenuFile_Separator1;
	private JMenuItem MenuFile_SaveAs;
	private JMenuItem MenuFile_Save;

	
	PopupMenu popupMenu;
	
	/**
	 * Panel na ktorym odbywa sie rysowanie
	 */
	private DrawingPanel MainWindowDrawing; 
	/**
	 * Pole w menu do rysowania Use Case
	 */
	private JMenuItem MenuTools_UseCase;
	private JMenuItem MenuTools_Inheritance;
	/**
	 * Button w menu do rysowania Use Case 
	 */;
	private JButton ToolBarUseCaseButton;
	private JMenuItem MenuTools_Selection;
	private JButton ToolBarSelectionButton;
	private JButton ToolBarActorButton;
	private JButton ToolBarIncludeButton;
	private JButton ToolBarExtendButton;
	private JButton ToolBarInheritanceButton;
	private JButton ToolBarBorderAreaUsesButton;
	private JButton ToolBarRealizeButton;
	private JButton ToolBarRefineButton;
	private JButton ToolBarUseCaseDashedButton;
	private JButton ToolBarMoveButton;
	/**   
	 * Drawing Tools
	 */
	LineTool PanelLineTool;
	UseCaseTool PanelUseCaseTool;
	SelectionTool PanelSelectionTool;
	ActorTool ActorCreationTool;
	IncludeTool IncludeCreationTool;
	ExtendTool ExtendCreationTool;
	InheritanceTool InheritanceCreationTool;
	BorderAreaUsesTool BorderAreaUsesCreationTool;
	RealizeTool RealizeCreationTool;
	RefineTool RefineCreationTool;
	UseCaseDashedTool UseDashedCreationTool;
	MoveTool MoveElementTool;
	
	///
	

	
	
	private void ChangeMainWindowDrawingMouseListener(MouseListener newMouseListener)
	{
		//remove(MainWindowDrawing);
		
		MouseListener[] PanelMouseListeners = MainWindowDrawing.getMouseListeners();
		
			
		for (int i = 0; i < PanelMouseListeners.length; ++i)
			MainWindowDrawing.removeMouseListener(PanelMouseListeners[i]);
		
		MainWindowDrawing.addMouseListener(newMouseListener);	
	}
	
	private void ChangeMainWindowDrawingMouseMotionListener(MouseMotionListener newMouseMotionListener)
	{
		MouseMotionListener[] PanelMouseMotionListeners = MainWindowDrawing.getMouseMotionListeners();
		
		for (int i = 0; i < PanelMouseMotionListeners.length; ++i)
			MainWindowDrawing.removeMouseMotionListener(PanelMouseMotionListeners[i]);
		
		MainWindowDrawing.addMouseMotionListener(newMouseMotionListener);
		
	}
	
	private JMenu jMenu1;
	private JMenuItem menuItem;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}


	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		DatabaseConnection.InitializeDatabaseConnection();
		DatabaseConnection.NewFile();
		
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainWindow inst = new MainWindow();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
		
	}
	
	public MainWindow() {
		super();
		initGUI();
		
		//initialize PanelLineTool
		PanelLineTool = new LineTool(MainWindowDrawing.GetGeometricObjects(), MainWindowDrawing);
		PanelUseCaseTool = new UseCaseTool(MainWindowDrawing.GetGeometricObjects(), MainWindowDrawing);
		PanelSelectionTool = new SelectionTool(MainWindowDrawing.GetGeometricObjects(), MainWindowDrawing);
		ActorCreationTool = new ActorTool(MainWindowDrawing.GetGeometricObjects(), MainWindowDrawing);
		IncludeCreationTool = new IncludeTool(MainWindowDrawing.GetGeometricObjects(), MainWindowDrawing);
		ExtendCreationTool = new ExtendTool(MainWindowDrawing.GetGeometricObjects(), MainWindowDrawing);
		 InheritanceCreationTool = new InheritanceTool(MainWindowDrawing.GetGeometricObjects(), MainWindowDrawing);
		 BorderAreaUsesCreationTool = new BorderAreaUsesTool(MainWindowDrawing.GetGeometricObjects(), MainWindowDrawing);
		 RealizeCreationTool = new RealizeTool(MainWindowDrawing.GetGeometricObjects(), MainWindowDrawing);
		 RefineCreationTool = new RefineTool(MainWindowDrawing.GetGeometricObjects(), MainWindowDrawing);
		 UseDashedCreationTool = new UseCaseDashedTool(MainWindowDrawing.GetGeometricObjects(), MainWindowDrawing);
		 MoveElementTool = new MoveTool(MainWindowDrawing.GetGeometricObjects(), MainWindowDrawing);
		//GeometricObjects GeomObjs = new GeometricObjects();
		
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setForeground(new java.awt.Color(0,0,0));
			getContentPane().setBackground(new java.awt.Color(176,217,225));
			{
				MainWindowDrawing = new DrawingPanel();
				getContentPane().add(MainWindowDrawing, BorderLayout.CENTER);
				MainWindowDrawing.setPreferredSize(new java.awt.Dimension(49, 10));
				MainWindowDrawing.setForeground(new java.awt.Color(0,255,0));
				MainWindowDrawing.setBackground(new java.awt.Color(200,200,255));
			}
			{
				ToolBar = new JToolBar();
				getContentPane().add(ToolBar, BorderLayout.NORTH);
				ToolBar.setPreferredSize(new java.awt.Dimension(370, 24));
				ToolBar.setFont(new java.awt.Font("Tahoma",0,7));
				{
					ToolBarLineButton = new JButton();
					ToolBar.add(ToolBarLineButton);
					ToolBarLineButton.setText("Line");
					ToolBarLineButton.setPreferredSize(new java.awt.Dimension(31, 11));
					ToolBarLineButton.setFont(new java.awt.Font("Tahoma",0,10));
					ToolBarLineButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							ToolBarLineButtonActionPerformed(evt);
						}
					});
				}
				{
					ToolBarUseCaseButton = new JButton();
					ToolBar.add(ToolBarUseCaseButton);
					ToolBarUseCaseButton.setText("Use Case");
					ToolBarUseCaseButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							ToolUseCaseButtonActionPerformed(evt);
							
						}
					});		
				}
				{
					ToolBarSelectionButton = new JButton();
					ToolBar.add(ToolBarSelectionButton);
					ToolBarSelectionButton.setText("Selection");
					
					ToolBarSelectionButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							ToolSelectionButtonActionPerformed(evt);
							
						}
					});		
				
				}
				{
					ToolBarActorButton = new JButton();
					ToolBar.add(ToolBarActorButton);
					ToolBarActorButton.setText("Actor");
					
					ToolBarActorButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							MenuTools_ActorSelectionPerformed(evt);
							
						}
					});		
				
				}
				
				{
					ToolBarIncludeButton = new JButton();
					ToolBar.add(ToolBarIncludeButton);
					ToolBarIncludeButton.setText("Include");
					
					ToolBarIncludeButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							MenuTools_IncludeSelectionPerformed(evt);
							
						}
					});		
				
				}
				{
					ToolBarExtendButton = new JButton();
					ToolBar.add(ToolBarExtendButton);
					ToolBarExtendButton.setText("Extend");
					
					ToolBarExtendButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							MenuTools_ExtendSelectionPerformed(evt);
							
						}
					});		
				
				}
				{
					ToolBarInheritanceButton = new JButton();
					ToolBar.add(ToolBarInheritanceButton);
					ToolBarInheritanceButton.setText("Inheritance");
					
					ToolBarInheritanceButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							ToolInheritanceActionPerformed(evt);
							
						}
					});		
				
				}
				
				{
					ToolBarBorderAreaUsesButton = new JButton();
					ToolBar.add(ToolBarBorderAreaUsesButton);
					ToolBarBorderAreaUsesButton.setText("BAU");
					
					ToolBarBorderAreaUsesButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							ToolBorderAreaUsesActionPerformed(evt);
							
						}
					});		
				
				}
				
				{
					ToolBarRealizeButton = new JButton();
					ToolBar.add(ToolBarRealizeButton);
					ToolBarRealizeButton.setText("Realize");
					
					ToolBarRealizeButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							ToolRealizeActionPerformed(evt);
							
						}
					});		
				
				}
				
				{
					ToolBarRefineButton = new JButton();
					ToolBar.add(ToolBarRefineButton);
					ToolBarRefineButton.setText("Refine");
					
					ToolBarRefineButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							ToolRefineActionPerformed(evt);
							
						}
					});		
				
				}
				
				{
					ToolBarUseCaseDashedButton = new JButton();
					ToolBar.add(ToolBarUseCaseDashedButton);
					ToolBarUseCaseDashedButton.setText("UCDashed");
					
					ToolBarUseCaseDashedButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							ToolUseCaseDashedActionPerformed(evt);
							
						}
					});		
				
				}
				
				{
					ToolBarMoveButton = new JButton();
					ToolBar.add(ToolBarMoveButton);
					ToolBarMoveButton.setText("Move");
					
					ToolBarMoveButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							ToolMoveActionPerformed(evt);
							
						}
					});		
				
				}
			}
			{
				MainWindowMenuBar = new JMenuBar();
				setJMenuBar(MainWindowMenuBar);
				{
					jMenu1 = new JMenu();
					FlowLayout jMenu1Layout = new FlowLayout();
					jMenu1.setLayout(jMenu1Layout);
					MainWindowMenuBar.add(jMenu1);
					jMenu1.setText("File");
					jMenu1.setPreferredSize(new java.awt.Dimension(32, 19));
					{
						MenuFile_Open = new JMenuItem();
						jMenu1.add(MenuFile_Open);
						MenuFile_Open.setText("Open");
					}
					{
						MenuFile_Save = new JMenuItem();
						jMenu1.add(MenuFile_Save);
						MenuFile_Save.setText("Save");
					}
					{
						MenuFile_SaveAs = new JMenuItem();
						jMenu1.add(MenuFile_SaveAs);
						MenuFile_SaveAs.setText("Save As");
					}
					{
						MenuFile_Separator1 = new JSeparator();
						jMenu1.add(MenuFile_Separator1);
					}
					{
						MenuFile_Close = new JMenuItem();
						jMenu1.add(MenuFile_Close);
						MenuFile_Close.setText("Close");
					}
					{
						
					}
				}
				{
					MenuWindow = new JMenu();
					MainWindowMenuBar.add(MenuWindow);
					MenuWindow.setText("Window");
				}
				{
					MenuTools = new JMenu();
					MainWindowMenuBar.add(MenuTools);
					MenuTools.setText("Tools");
					{
						MenuTools_Line = new JMenuItem();
						MenuTools.add(MenuTools_Line);
						MenuTools_Line.setText("Line");
						MenuTools_Line.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								MenuTools_LineActionPerformed(evt);
							}
						});
					}
					{
						MenuTools_UseCase = new JMenuItem();
						MenuTools.add(MenuTools_UseCase);
						MenuTools_UseCase.setText("Use Case");
						MenuTools_UseCase.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								MenuTools_UseCaseActionPerformed(evt);
							}
						});
					}
					{
						MenuTools_Selection = new JMenuItem();
						MenuTools.add(MenuTools_Selection);
						MenuTools_Selection.setText("Selection");
						MenuTools_Selection.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								ToolExtendButtonActionPerformed(evt);
							}
						});
					}
					
					{
						MenuTools_Inheritance= new JMenuItem();
						MenuTools.add(MenuTools_Inheritance);
						MenuTools_Inheritance.setText("Inheritance");
						MenuTools_Inheritance.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								MenuTools_InheritanceSelectionPerformed(evt);
							}
						});
					}
					
					
				}
			}
			addMouseListener(new MouseAdapter(){
				public void mousePressed(MouseEvent Me)
			    {
			        if(Me.isPopupTrigger())
			        {
			          popupMenu.show(Me.getComponent(), Me.getX(), Me.getY());
			        }
			    }
			    public void mouseReleased(MouseEvent Me)
			    {
			        if(Me.isPopupTrigger())
			        {
			          popupMenu.show(Me.getComponent(), Me.getX(), Me.getY());
			        }
			    }
			}
			);
			
			popupMenu = new PopupMenu();;
			pack();
			this.setSize(800, 600);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//

		 
	}
	
	/**
	 * 
	 * @param evt
	 * uruchamianie narzedzia dodawania linii
	 */
	private void MenuTools_LineActionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(PanelLineTool);
		ChangeMainWindowDrawingMouseMotionListener(PanelLineTool);
		System.out.println("Line Tool Start");
	}
	

	private void ToolBarLineButtonActionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(PanelLineTool);
		ChangeMainWindowDrawingMouseMotionListener(PanelLineTool);
		System.out.println("Line Tool Start");
	}
	
	/**
	 * 
	 * @param evt
	 * uruchamianie narzedzia dodawania UseCase
	 */
	private void MenuTools_UseCaseActionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(PanelUseCaseTool);
		ChangeMainWindowDrawingMouseMotionListener(PanelUseCaseTool);
		System.out.println("UseCase Tool Start");
	}

	private void ToolUseCaseButtonActionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(PanelUseCaseTool);
		ChangeMainWindowDrawingMouseMotionListener(PanelUseCaseTool);
		System.out.println("UseCase Tool Start");
	}
	
	/**
	 * 
	 * @param evt
	 * uruchamianie narzedzia Selekcji
	 */
	private void MenuTools_SelectionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(PanelSelectionTool);
		ChangeMainWindowDrawingMouseMotionListener(PanelSelectionTool);
		System.out.println("Selection Tool Start");
	}
	

	private void ToolSelectionButtonActionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(PanelSelectionTool);
		ChangeMainWindowDrawingMouseMotionListener(PanelSelectionTool);
		System.out.println("Selection Tool Start");
	}
	
	/**
	 * 
	 * @param evt
	 * uruchamianie narzedzia rysowania aktora
	 */
	private void MenuTools_ActorSelectionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(ActorCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(ActorCreationTool);
		System.out.println("Selection Tool Start");
	}
	

	private void ToolActorButtonActionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(ActorCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(ActorCreationTool);
		System.out.println("Selection Tool Start");
	}
	
	/**
	 * 
	 * @param evt
	 * uruchamianie narzedzia rysowania Includes
	 */
	private void MenuTools_IncludeSelectionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(IncludeCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(IncludeCreationTool);
		System.out.println("Selection Tool Start");
	}
	
	private void ToolIncludeButtonActionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(IncludeCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(IncludeCreationTool);
		System.out.println("Selection Tool Start");
	}
	
	/**
	 * 
	 * @param evt
	 * uruchamianie narzedzia rysowania extends
	 */
	private void MenuTools_ExtendSelectionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(ExtendCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(ExtendCreationTool);
		System.out.println("Selection Tool Start");
	}
	

	private void ToolExtendButtonActionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(ExtendCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(ExtendCreationTool);
		System.out.println("Selection Tool Start");
	}
	
	/**
	 * 
	 * @param evt
	 * uruchamianie narzedzia rysowania include
	 */
	private void MenuTools_InheritanceSelectionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(InheritanceCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(InheritanceCreationTool);
		System.out.println("Selection Tool Start");
	}

	private void ToolInheritanceActionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(InheritanceCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(InheritanceCreationTool);
		System.out.println("Selection Tool Start");
	}
	
	/**
	 * 
	 * @param evt
	 * uruchamianie narzedzia rysowania include
	 */
	private void MenuTools_BorderAreaUsesSelectionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(BorderAreaUsesCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(BorderAreaUsesCreationTool);
		System.out.println("Selection Tool Start");
	}

	private void ToolBorderAreaUsesActionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(BorderAreaUsesCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(BorderAreaUsesCreationTool);
		System.out.println("Selection Tool Start");
	}
	
	/**
	 * 
	 * @param evt
	 * uruchamianie narzedzia rysowania include
	 */
	private void MenuTools_RealizeSelectionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(RealizeCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(RealizeCreationTool);
		System.out.println("Selection Tool Start");
	}

	private void ToolRealizeActionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(RealizeCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(RealizeCreationTool);
		System.out.println("Selection Tool Start");
	}
	/**
	 * 
	 * @param evt
	 * uruchamianie narzedzia rysowania include
	 */
	private void MenuTools_RefineSelectionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(RefineCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(RefineCreationTool);
		System.out.println("Selection Tool Start");
	}

	private void ToolRefineActionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(RefineCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(RefineCreationTool);
		System.out.println("Selection Tool Start");
	}
	
	
	/**
	 * 
	 * @param evt
	 * uruchamianie narzedzia rysowania include
	 */
	private void MenuTools_UseCaseDashedSelectionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(UseDashedCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(UseDashedCreationTool);
		System.out.println("Selection Tool Start");
	}

	private void ToolUseCaseDashedActionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(UseDashedCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(UseDashedCreationTool);
		System.out.println("Selection Tool Start");
	}
	
	/**
	 * 
	 * @param evt
	 * uruchamianie narzedzia rysowania include
	 */
	private void MenuTools_MoveSelectionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(UseDashedCreationTool);
		ChangeMainWindowDrawingMouseMotionListener(UseDashedCreationTool);
		System.out.println("Selection Tool Start");
	}

	private void ToolMoveActionPerformed(ActionEvent evt) {
		ChangeMainWindowDrawingMouseListener(MoveElementTool);
		ChangeMainWindowDrawingMouseMotionListener(MoveElementTool);
		System.out.println("Selection Tool Start");
	}
}
